using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Charisma.Common.Disposable;
using Charisma.MongoDb.Linq.Expressions;

namespace Charisma.MongoDb.Linq
{
  internal sealed class TranslatorState
  {
    private readonly Translator translator;
    public bool BuildingProjection { get; set; }
    public bool BuildingField { get; set; }

    public IDisposable CreateScope()
    {
      var currentState = translator.state;
      var newState = new TranslatorState(currentState);
      translator.state = newState;
      return new Disposable(_ => translator.state = currentState);
    }

    [ContractInvariantMethod]
    private void ObjectInvariant()
    {
      Contract.Invariant(translator != null);
    }


    public TranslatorState(Translator translator)
    {
      Contract.Requires(translator != null);
      this.translator = translator;
    }

    private TranslatorState(TranslatorState state)
    {
      Contract.Requires(state != null);

      translator = state.translator;
      BuildingProjection = state.BuildingProjection;
      BuildingField = state.BuildingField;
    }
  }
}